//
//  JXLComposeView.swift
//  MicroBlog
//
//  Created by 贾晓磊 on 16/10/5.
//  Copyright © 2016年 贾晓磊. All rights reserved.
//

import UIKit
import pop
//罩层视图
class JXLComposeView: UIView {

    //纪录当前出入的控制器
    var target: UIViewController?
    
    //MARK: 懒加载控件
    private lazy var sloganImageView: UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))
    
    //纪录添加的6个button按钮
    private lazy var buttonArray: [JXLComposeButton] = [JXLComposeButton]()
    
    //  取到plist对象的模型数据
    private lazy var composeMenuArray: [JXLComposeMenu] = self.loadComposePlistData()
    
        
    //重写init方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.size = CGSize(width: ScreenWidth, height: ScreenHeight)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        backgroundColor = randomColor()
        //获取模糊效果图片
        let image = UIImage.screenShot().applyLightEffect()
        
        let bgImageView = UIImageView(image: image)
        
        addSubview(bgImageView)
        
        addSubview(sloganImageView)
        
        //sloganImageView 设置约束
        sloganImageView.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(100)
            make.centerX.equalTo(self)
        }
        
        //添加文字按钮
//        let button = JXLComposeButton()
//        button.size = CGSize(width: 80, height: 110)
//        button.setImage(UIImage(named: "tabbar_compose_idea"), for: .normal)
//        button.setTitle("文字", for: .normal)
        addChildButton()

        
        
    }
    
    //开动弹簧动画效果
    private func startPopAnimation(isUp: Bool) {
    
        //如果是向下的弹簧动画，就反转按钮数组
        if isUp == false {
            buttonArray = buttonArray.reversed()
        }
        
    for (i, button) in buttonArray.enumerated() {
     
         let popAnimation = POPSpringAnimation(propertyNamed: kPOPViewCenter)
        
        if isUp == true {
            //目的地toValue 不能直接穿入结构体，需要转成对象
            popAnimation?.toValue = NSValue(cgPoint: CGPoint(x: button.centerX, y: button.centerY - 350))
        } else {
           popAnimation?.toValue = NSValue(cgPoint: CGPoint(x: button.centerX, y: button.centerY + 350))
        }
        
        
        //动画的初始速度 [0,20]
        popAnimation?.springSpeed = 10
        //动画的抖动幅度
        popAnimation?.springBounciness = 10
        //开始动画时间,CACurrentMediaTime()系统绝对时间
        popAnimation?.beginTime = CACurrentMediaTime() + Double(i) * 0.025
        //执行动画
        button.pop_add(popAnimation, forKey: nil)
    }
    
    }
    
    //  添加子按钮
    private func addChildButton() {
        //  按钮的宽度
        let buttonWidth: CGFloat = 80
        //  按钮的高度
        let buttonHeight: CGFloat = 110
        //  按钮的间距
        let buttonMargin: CGFloat = (width - 3 * buttonWidth) / 4
        
        for i in 0..<composeMenuArray.count {
            //  列的索引
            let colIndex = i % 3
            //  行的索引
            let rowIndex = i / 3
            
            //获取指定下标模型
            let composeMenu = composeMenuArray[i]
            
            
            let button = JXLComposeButton()
            //  设置tag值
            button.tag = i

            button.addTarget(self, action: #selector(buttonAction(btn:)), for: .touchUpInside)
            button.x = CGFloat(colIndex) * buttonWidth + CGFloat(colIndex + 1) * buttonMargin
            button.y = CGFloat(rowIndex) * buttonHeight + CGFloat(rowIndex) * buttonMargin + ScreenHeight
            
            button.size = CGSize(width: buttonWidth, height: buttonHeight)
            button.setImage(UIImage(named: composeMenu.icon!), for: .normal)
            button.setTitle(composeMenu.title!, for: .normal)

            addSubview(button)
            
            buttonArray.append(button)
        }
    }
//    //截取当前window的图片
//    func screenShot() -> UIImage {
//        
//        let window = UIApplication.shared.keyWindow!
//        //  开启图片上下文
//        UIGraphicsBeginImageContext(window.size)
//        
//        //  afterScreenUpdates -> false 表示不需要等待更新后去绘制
//        window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
//        
//        //  从图片上下文中获取图片
//        let image = UIGraphicsGetImageFromCurrentImageContext()
//        
//        UIGraphicsEndImageContext()
//        
//        return image!
//    }
    
    //MARK: 点击事件处理
   @objc private func buttonAction(btn: UIButton) {
    
    UIView.animate(withDuration: 0.25, animations: { 
        
        for button in self.buttonArray {
            button.alpha = 0.2
            if button == btn {
                //表示同一个按钮，执行放大动画
                button.transform = CGAffineTransform(scaleX: 2, y: 2)
            } else {
                //执行缩小动画
                button.transform = CGAffineTransform(scaleX: 0.2, y: 0.2)
            }
        }
        
        }) { (_) in
            
            UIView.animate(withDuration: 0.25, animations: { 
                for button in self.buttonArray {
                    //回到原始大小
                    button.transform = .identity
                    button.alpha = 1
                }
                
                }, completion: { (_) in
                    //弹出控制器
                    //let composeVc = JXLComposeViewController()
                    //  1. 自定义的类直接通过类名转成相应class不行
                    //  -> 解决办法 需要加上命名空间(工程名)
                    //  2. 转成的class不能直接初始化,需要转成相应的类型 (类名.Type)
                    
                    //  3. 最后就通过class转成相应的对象

                    //获取指定模型
                    let composeMenu = self.composeMenuArray[btn.tag]
                    //获取对应的class
                    let classType = NSClassFromString(composeMenu.className!)! as! UIViewController.Type
                    //  根据class创建对象
                    let vc = classType.init()
                    //vc 放到导航控制器中
                    let nav = UINavigationController(rootViewController: vc)
                    self.target?.present(nav, animated: true, completion: {
                        //移除罩层
                        self.removeFromSuperview()
                    })
                    
            })
            
    }
    
    }
    
    //
    func show(target: UIViewController) {
        //纪录当前出入的控制器
        self.target = target
        
        //最上层的window
//        let window = UIApplication.shared.windows.last!
//        window.addSubview(self)
        target.view.addSubview(self)
        //开启弹簧动画效果
        startPopAnimation(isUp: true)
    }
    
    //读取plist文件数据
    func loadComposePlistData() -> [JXLComposeMenu] {
        //获取plist文件路径
        let path = Bundle.main.path(forResource: "compose.plist", ofType: nil)!
        //  获取到plist文件里面的数据
        let dicArray = NSArray(contentsOfFile: path)!
        
      let modelArray = NSArray.yy_modelArray(with: JXLComposeMenu.self, json: dicArray)! as! [JXLComposeMenu]
        
        return modelArray
        
    }
    
    //点击从父控件移除
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        startPopAnimation(isUp: false)
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) {
            self.removeFromSuperview()
        }
    }
}
